<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentViewPicture.class.php, $Author:  $ */



/** Component for view group pictuer
 * @version $Id: PhpFox_ComponentViewPicture.class.php 
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentViewPicture extends BaseComponent
{
    /** Processing method
     */
    
    function process()
    {
        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('read_only', 'full'));
		
        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

         $oGroupsGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGroupsGalleryService PhpFox_Mod_Groups_ServiceGallery */
         
        App::loadClass('Pager');
        
        $oReq = &App::getRequest();
        
        $oUrl = &App::getUrl();
        /* @var $oUrl Url */ 
        
        $oTpl = $this->_oModule->getTemplate();

        $iGroupId = $oReq->getInt('id', 0);
        $iImageId = $oReq->getInt('image', 0);
        $iDefaultId = $oReq->getInt('default', 0);
        $iDeleteId = $oReq->getInt('delete', 0);
        $oUser = $this->getParam('user');
        
        if($iGroupId)
        {
            //check permissions
//             function isMember($iGroupId, $sUser, $iRecordId = 0)
	        $aGroupData = $oGroupsService->getGroupData($iGroupId);
	        
            if(!($oGroupsService->isMember($iGroupId, App::getUser('user')) OR App::isStaff()) && $aGroupData['hide_group'] == 'Yes')
                App::gotoUrl('public.groups.private',array('id'=>$iGroupId));
            
            //Picture details
            if($iImageId && $iDefaultId == 0 && $iDeleteId == 0)
            {
		        list($aPrevImage, $aImage, $aNextImage) = $oGroupsGalleryService->getImagesPrevNext($iGroupId, $iImageId);
		        
		        App::setAccessForContent('groups', $iImageId);
		        $iWidth = 0;
		        $iHeight = 0;
				$sActualImage = App::getParam('groups.pic.url') . $iImageId . '_view.jpg';
				if (!file_exists(App::getParam('groups.pic.path') . $iImageId . '_view.jpg'))
				{
					$sActualImage = App::getParam('groups.pic.url') . $iImageId . '.jpg';
				}				
				
				if (file_exists(App::getParam('groups.pic.path') . $iImageId . '.jpg'))
				{
					list($iWidth, $iHeight) = getimagesize(App::getParam('groups.pic.path') . $iImageId . '.jpg');				
				}
		
		        $oTpl->assign(array(
		            'bShowPrev' => count($aPrevImage),
		            'bShowNext' => count($aNextImage),
		            'aGroupData'=> $aGroupData,
		            'aImage'	=> $aImage,
		            'aPrevImage'=> $aPrevImage,
			        'aNextImage'=> $aNextImage,
			        'sImageUrl' 	=> App::getParam('groups.pic.url'), 
			        'sActualImage'	=> $sActualImage,
                    'iImgViewSize' 	=> App::getParam('upload_set_size_view'),
            		'iWidth'           => $iWidth,
            		'iHeight'          => $iHeight
		        ));
    	        $sHtml = $oTpl->fetch('ViewPicture.html');
            }
            else
            {
		        if (App::getParam('bImageSecurity'))
				{
		        	App::clearAccessForContent('groups');
				}                
            	
                $bIsGroupOwner = $oGroupsService->isOwner($iGroupId, App::getUser('user'));

                if($bIsGroupOwner OR APP::isStaff())
                {
	                if($iDefaultId)
	                {
	                    if($oGroupsGalleryService->isGroupImage($iGroupId, $iDefaultId))
	                        $oGroupsGalleryService->setDefaultImage($iGroupId, $iDefaultId);
	                }
                }
                
                if($iDeleteId && ($bIsGroupOwner || $oGroupsGalleryService->isImageOwner($iDeleteId, $oUser->aData['user']) || APP::isStaff()))
	            {
	                    if($oGroupsGalleryService->isGroupImage($iGroupId, $iDeleteId))
    	                    $oGroupsGalleryService->deleteItem($iDeleteId);
	            }
                
                //Picture list
                $iPageSize = 9;

                $iPage = $oReq->getInt('page', 1);
		        list($iCnt, $aImages) = $oGroupsGalleryService->getImages($iGroupId, $iPage, $iPageSize);
		        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
		        $iFrom = $oPager->getOffset($iPage, $iPageSize);
		        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize); 
		        
                $iColspan = ($iTo - $iFrom) % 4;

		        $oTpl->assign(array(
		            'sUser'        => App::getUser('user'),
		            'bIsGroupOwner' => $bIsGroupOwner,
		            'iColspan'  => $iColspan,
		            'aGroupData'=> $aGroupData,
		            'aImages'	=> $aImages,
		            'iTo'       => $iTo,
		            'iFrom'     => ($iCnt) ? ($iFrom + 1) : 0,
		            'iCnt'      => $iCnt,
		            'aPaging'       => $oPager->getInfo($oUrl),
			        'sImageUrl' 	=> App::getParam('groups.pic.url'), 
			        'bIsAdmin'	=> App::isStaff(),
		        ));
		        
		        $sHtml = $oTpl->fetch('ListPictures.html');
		        
            }
        }
        return $sHtml;
    }
}
?>